package com.atguigu.srb.core.controller.admin;


import com.atguigu.srb.base.asserts.Asserts;
import com.atguigu.srb.base.exception.BusinessException;
import com.atguigu.srb.base.response.R;
import com.atguigu.srb.base.response.ResponseEnum;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2023-05-26
 */
@RestController
@Api(tags = "积分等级管理模块")
@Slf4j
//@CrossOrigin //允许跨域访问
@RequestMapping("/admin/core/integralGrade")
public class AdminIntegralGradeController {
    @Resource
    IntegralGradeService integralGradeService;
    //批量删除的id集合：
    //方式1： url?ids=1&ids=2&ids=3  或者  url?ids=1,2,3  接收：@RequestParam("ids") List<String> ids
    //方式2： url + 请求体json字符串( [1,2,3,4,5]  )  接收：@RequestBody  List<String> ids
    @ApiOperation("批量删除积分等级")
    @DeleteMapping("batchDel")
    public R batchDel(@RequestBody List<String> ids){
        integralGradeService.removeByIds(ids);
        return R.ok();
    }
    //1、查询所有
    @GetMapping
    public R list() {
        log.debug("{}级别的日志,当前时间：{}", "debug", new Date());
        log.info("{}级别的日志,当前时间：{}", "info", new Date());
        log.warn("{}级别的日志,当前时间：{}", "warn", new Date());
        log.error("{}级别的日志,当前时间：{}", "error", new Date());
        return R.ok().data("items", integralGradeService.list());
    }

    //2、分页查询
    @ApiOperation(value = "分页查询")
    @GetMapping("page/{pageNum}/{pageSize}")
    public R page(@ApiParam(value = "页码", example = "1") @PathVariable("pageNum") Integer pageNum,
                  @ApiParam(value = "每页记录数", example = "2") @PathVariable("pageSize") Integer pageSize,
                  @ApiParam(value = "条件", example = "10") @RequestParam("key") Integer key) {//查询积分等级 起始积分超过key的分页数据
        IPage<IntegralGrade> page = new Page<>(pageNum, pageSize);
//        integralGradeService.page(page);
        integralGradeService.queryIntegralGradesByKey(page, key);
        return R.ok().data("page", page);
    }

    //3、新增积分等级
    //mp可以在新增或者更新时 让mybatis的拦截器拦截请求 设置时间值到对象中  因为每个表数据新增更新时 都要操作相同属性 设置相同的值
    @PostMapping
    public R save(@RequestBody IntegralGrade integralGrade) {
//        if(integralGrade.getIntegralStart()==null ||
//            integralGrade.getIntegralEnd()==null||
//            integralGrade.getBorrowAmount()==null){
//           throw new BusinessException(ResponseEnum.PARAM_ERROR);
//        }
        Asserts.assertNotNull(integralGrade.getIntegralStart(), ResponseEnum.PARAM_ERROR);
        Asserts.assertNotNull(integralGrade.getIntegralEnd(), ResponseEnum.PARAM_ERROR);
        Asserts.assertNotNull(integralGrade.getBorrowAmount(), ResponseEnum.PARAM_ERROR);
//        integralGrade.setId(); id后端通过mp的雪花算法自动生成
//        integralGrade.setCreateTime(); 后端设置  mp的自动填充  统一配置
//        integralGrade.setUpdateTime();
//        integralGrade.setCreateTime(new Date());
//        integralGrade.setUpdateTime(new Date());
        integralGradeService.update(Wrappers.lambdaUpdate(IntegralGrade.class)
                .set(IntegralGrade::getBorrowAmount , 1000)
                .eq(IntegralGrade::getId , 1));
        return R.ok().data("flag", integralGradeService.save(integralGrade));
    }

    //4、删除积分等级
    @DeleteMapping("{id}")
    public R removeById(@PathVariable("id") Long id) {

        return R.ok().data("flag", integralGradeService.removeById(id));
    }

    //6、根据id查询积分等级
    @GetMapping("{id}")
    public R getById(@PathVariable("id") Long id) {

        return R.ok().data("item", integralGradeService.getById(id));
    }

    //5、更新积分等级
    @PutMapping
    public R updateById(@RequestBody IntegralGrade integralGrade) {
        Asserts.assertNotNull(integralGrade.getId(), ResponseEnum.PARAM_ERROR);
        return R.ok().data("flag", integralGradeService.updateById(integralGrade));
    }

}

